package com.hljx.controller;

import com.hljx.config.Result;
import com.hljx.config.exception.DataException;
import com.hljx.dto.DateDTO;
import com.hljx.entity.AssessmentCycle;
import com.hljx.entity.EtlItemDetail;
import com.hljx.mapper.gy.GYItemMapper;
import com.hljx.service.AssessmentCycleService;
import com.hljx.service.EtlItemDetailService;
import com.hljx.service.ThreadService;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.coyote.Response;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 数据抽取明细表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-10-27
 */
@RestController
@RequestMapping("/etlItemDetail")
public class EtlItemDetailController {

    @Autowired
    private EtlItemDetailService  etlItemDetailService;
    @Autowired
    private   ThreadService  threadService;
    @Autowired
    private GYItemMapper gyItemMapper;
    @Autowired
    private AssessmentCycleService  assessmentCycleService;

    @Operation(summary = "数据清洗")
    @PostMapping ("/dataCleaning")
    public Result dataCleaning(@RequestBody DateDTO dateDTO) {
        AssessmentCycle assessmentCycle = assessmentCycleService.getById(dateDTO.getId());
       if (assessmentCycle.getIsFile()==0){

        new Thread(() -> {
            try {
                threadService.dataAsync(dateDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        return Result.success("开始清洗");
       }else {
        return Result.failed("周期已存档");
       }
    }




    @Operation(summary = "数据计算")
    @PostMapping ("/dataStatistics")
    public Result dataStatistics(@RequestBody DateDTO dateDTO) {
        new Thread(() -> {
            try {
                threadService.dataAsync(dateDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        return Result.success("开始计算");
    }


    @Operation(summary = "数据抽取")
    @PostMapping ("/dataDecimation")
    public Result dataDecimation(@RequestBody DateDTO dateDTO)  {
        AssessmentCycle assessmentCycle = assessmentCycleService.getById(dateDTO.getId());
        if (assessmentCycle.getIsFile()==0){
        new Thread(() -> {
            try {
          etlItemDetailService.dataDecimation(dateDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        return Result.success("开始抽取");
        }else {
            return Result.failed("周期已存档");
        }
    }



    @Operation(summary = "供应室数据查询")
    @PostMapping ("/selectAllByGY")
    public Result selectAllByGY(@RequestBody DateDTO dateDTO)  {

        List<EtlItemDetail> etlItemDetailList = gyItemMapper.selectAllByGY(dateDTO);

        return Result.success(etlItemDetailList);
    }

}
